﻿using System.Collections.Generic;
using CoreVirtuoso.Descriptions;
using Infrastructure.Helpers;
using Infrastructure.Lgd.Interfaces;
using NGenerics.DataStructures;

namespace Infrastructure.Lgd
{
   //ToDo: Add as singleton dependency
   public class LgdTreeFilteringService : ILgdTreeFilteringService
   {
      private ILgdTreeBuilder _lgdTreeBuilder;
      private ILgdTreeFilter _lgdTreeFilter;

      private static GeneralTree<string> _categoriesTreeRootNode;

      public LgdTreeFilteringService(ILgdTreeBuilder lgdTreeBuilder, ILgdTreeFilter lgdTreeFilter)
      {
         _lgdTreeBuilder = lgdTreeBuilder;
         _lgdTreeFilter = lgdTreeFilter;
      }

      public IList<GeneralTree<EntityDescription>> GetCategoriesTree(List<string> categoriesToFilter)
      {
         if (_categoriesTreeRootNode == null)
            _categoriesTreeRootNode = _lgdTreeBuilder.GetCategoriesTree();
        
         return _lgdTreeFilter.FilterCategoriesTree(_categoriesTreeRootNode, categoriesToFilter).ChildNodes;
      }
   }
}
